package com.micro.user.controller;

import com.micro.user.entity.Passenger;
import com.micro.user.repository.PassengerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 乘客控制器
 * 
 * @author micro
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/passengers")
public class PassengerController {

    @Autowired
    private PassengerRepository passengerRepository;

    /**
     * 根据用户ID获取乘客列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Passenger>> getPassengersByUserId(@PathVariable Long userId) {
        try {
            // 使用MyBatis Plus的查询构造器
            List<Passenger> passengers = passengerRepository.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Passenger>()
                    .eq("user_id", userId)
                    .eq("active", true)
            );
            return ResponseEntity.ok(passengers);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 根据身份证号查找乘客
     */
    @GetMapping("/search")
    public ResponseEntity<Passenger> searchPassenger(@RequestParam String idCardNumber) {
        try {
            Passenger passenger = passengerRepository.selectOne(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Passenger>()
                    .eq("id_card_number", idCardNumber)
                    .eq("active", true)
            );
            if (passenger == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(passenger);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 添加乘客信息
     */
    @PostMapping
    public ResponseEntity<Passenger> addPassenger(@RequestBody Passenger passenger) {
        try {
            // 检查身份证号是否已存在
            Passenger existingPassenger = passengerRepository.selectOne(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Passenger>()
                    .eq("id_card_number", passenger.getIdCardNumber())
            );
            if (existingPassenger != null) {
                return ResponseEntity.badRequest().build();
            }
            
            // 设置默认值
            if (passenger.getActive() == null) {
                passenger.setActive(true);
            }
            
            passengerRepository.insert(passenger);
            return ResponseEntity.ok(passenger);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 更新乘客信息
     */
    @PutMapping("/{passengerId}")
    public ResponseEntity<Passenger> updatePassenger(
            @PathVariable Long passengerId,
            @RequestBody Passenger passenger
    ) {
        try {
            passenger.setPassengerId(passengerId);
            
            // 检查乘客是否存在
            Passenger existingPassenger = passengerRepository.selectById(passengerId);
            if (existingPassenger == null) {
                return ResponseEntity.notFound().build();
            }
            
            passengerRepository.updateById(passenger);
            return ResponseEntity.ok(passenger);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 删除乘客信息
     */
    @DeleteMapping("/{passengerId}")
    public ResponseEntity<Void> deletePassenger(@PathVariable Long passengerId) {
        try {
            // 检查乘客是否存在
            Passenger existingPassenger = passengerRepository.selectById(passengerId);
            if (existingPassenger == null) {
                return ResponseEntity.notFound().build();
            }
            
            passengerRepository.deleteById(passengerId);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 统计指定用户的乘客数量
     */
    @GetMapping("/count/user/{userId}")
    public ResponseEntity<Long> getPassengerCountByUserId(@PathVariable Long userId) {
        try {
            Long count = passengerRepository.selectCount(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Passenger>()
                    .eq("user_id", userId)
                    .eq("active", true)
            );
            return ResponseEntity.ok(count);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 测试端点 - 获取所有乘客
     */
    @GetMapping("/test/all")
    public ResponseEntity<List<Passenger>> getAllPassengers() {
        try {
            List<Passenger> passengers = passengerRepository.selectList(null);
            return ResponseEntity.ok(passengers);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
